package org.codeart.iterator.example01;

import java.util.List;
import java.util.NoSuchElementException;

public class ConcreteIterator<E> implements Iterator<E> {

    // 游标
    private int cursor;

    // 容器
    private final List<E> container;

    public ConcreteIterator(List<E> container) {
        this.cursor = -1;
        this.container = container;
    }

    @Override
    public boolean hasNext() {
        return cursor < container.size() - 1;
    }

    @Override
    public E next() {
        if (cursor >= container.size()) {
            throw new NoSuchElementException();
        }
        return container.get(++cursor);
    }
}
